package com.example.zyfyptlzx729.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import com.example.zyfyptlzx729.R;
import com.example.zyfyptlzx729.activities.ViewtcaseActivity;
import com.example.zyfyptlzx729.bean.CollectBean;
import com.example.zyfyptlzx729.bean.TcaseBean;
import com.example.zyfyptlzx729.common.Common;
import com.squareup.picasso.Picasso;

import java.util.List;

public class CollectTcaseAdapter extends RecyclerView.Adapter {

    private Context context;//上下文
    private LayoutInflater layoutInflater;//动态加载布局
    private List<CollectBean<TcaseBean>> list;//保存要显示的数据


    //1自定义：构造方法，传进上下文
    public CollectTcaseAdapter(Context context) {
        this.context=context;
        layoutInflater= LayoutInflater.from(context);
    }
    //2自定义：获取数据
    public void setList(List<CollectBean<TcaseBean>> list)
    {
        this.list=list;
    }
    //3自定义：ViewHolder子类
    public class ViewHolder extends RecyclerView.ViewHolder
    {  ImageView imageView;
        TextView tvtile,tvauthor,tvtime;
        public ViewHolder(View itemView) {

            super(itemView);
            imageView=itemView.findViewById(R.id.imageView2);
            tvtile=itemView.findViewById(R.id.textView4);
            tvauthor=itemView.findViewById(R.id.textView6);
            tvtime=itemView.findViewById(R.id.textView10);
        }
    }
    //4重写：生成Item的View
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v=layoutInflater.inflate(R.layout.item,parent,false);
        return new ViewHolder(v);
    }
    //5重写：给ViewHolder中的控件填充值，设置监听
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final TcaseBean commonBean=list.get(position).getBean();
        if(commonBean==null)
            return;
        final ViewHolder viewHolder=(ViewHolder)holder;
        Picasso.with(context)
                .load(Common.IMAGEURL+commonBean.getThumb())
                .placeholder(R.mipmap.ic_launcher)
                .into(viewHolder.imageView);
        viewHolder.tvtile.setText(commonBean.getName());
        viewHolder.tvauthor.setText(commonBean.getAuthor());
        viewHolder.tvtime.setText(commonBean.getUpdate_time());
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int id=list.get(position).getResid();
                Toast.makeText(context,""+id,Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(context, ViewtcaseActivity.class);
                intent.putExtra("resid",list.get(position).getResid());
                intent.putExtra("idolid",list.get(position).getUserid());
                context.startActivity(intent);
            }
        });
    }
    //共有多少条目
    @Override
    public int getItemCount() {
        if(list==null)  return 0;
        else
            return list.size();
    }
}
